package model

import (
	"gorm.io/gorm"
)

// 橱窗
type ShopWindow struct {
	gorm.Model
	UserId     int    `gorm:"type:int;index;comment:达人ID"`               //达人ID
	Name       string `gorm:"type:varchar(100);comment:橱窗名称"`            //橱窗名称
	Image      string `gorm:"type:varchar(255);comment:橱窗图片"`            //橱窗图片
	Desc       string `gorm:"type:varchar(255);comment:橱窗描述"`            //橱窗描述
	Status     int    `gorm:"type:int;default:1;comment:橱窗状态;1-正常,2-下架"` //橱窗状态
	CategoryId int    `gorm:"type:int;index;comment:橱窗分类"`               //橱窗分类
}

// 橱窗分类
type ShopWindowCategory struct {
	gorm.Model
	Name string `gorm:"type:varchar(100);comment:分类名称"` //分类名称
	Desc string `gorm:"type:varchar(255);comment:分类描述"` //分类描述
}

// 商品
type Goods struct {
	gorm.Model
	Name        string `gorm:"type:varchar(100);comment:商品名称"`                      //商品名称
	Image       string `gorm:"type:varchar(255);comment:商品图片"`                      //商品图片
	Desc        string `gorm:"type:varchar(255);comment:商品描述"`                      //商品描述
	Price       int    `gorm:"type:int;comment:商品价格"`                               //商品价格
	Stock       int    `gorm:"type:int;comment:商品库存"`                               //商品库存
	Status      int    `gorm:"type:int;default:1;comment:商品状态:1-正常,2-下架"`           //商品状态
	CheckStatus int    `gorm:"type:int;default:1;comment:审核状态:1-待审核,2-审核通过,3-审核拒绝"` //审核状态
}

// 商品关联橱窗
type GoodsShopWindow struct {
	gorm.Model
	ShopWindowId int    `gorm:"type:int;index;comment:橱窗ID"`               //橱窗ID
	GoodsId      int    `gorm:"type:int;index;comment:商品ID"`               //商品ID
	UserId       int    `gorm:"type:int;index;comment:达人ID"`               //达人ID
	Name         string `gorm:"type:varchar(100);comment:商品名称"`            //商品名称
	Image        string `gorm:"type:varchar(255);comment:商品图片"`            //商品图片
	Desc         string `gorm:"type:varchar(255);comment:商品描述"`            //商品描述
	Price        int    `gorm:"type:int;comment:商品价格"`                     //商品价格
	Stock        int    `gorm:"type:int;comment:商品库存"`                     //商品库存
	Status       int    `gorm:"type:int;default:1;comment:商品状态;1-正常,2-下架"` //商品状态
}

// 橱窗列表
type ShopWindowsList struct {
	ShopWindowsName string `json:"shop_windows_name"`
	Username        string `json:"username"`
	CategoryName    string `json:"category_name"`
	Status          int    `json:"status"`
	CreatedAt       string `json:"created_at"`
}

// 商品销量增量更新
type ProductSales struct {
	ProductId string `json:"product_id"`
	Sales     int64  `json:"sales"`
}

func (psu *ProductSales) ProductSalesUpdate(db *gorm.DB) error {
	return db.Model(&Goods{}).Where("id = ?", psu.ProductId).Update("stock", gorm.Expr("stock + ?", psu.Sales)).Error
}

// 获取橱窗列表
func (sw *ShopWindow) GetShopWindows(db *gorm.DB, page, pageSize int, categoryId int, name string) (shopWindowsList []*ShopWindowsList, err error) {
	err = db.Model(&ShopWindow{}).
		Select("shop_window.name as shop_windows_name,user.username,shop_window_categories.name as category_name,shop_window.status,shop_window.created_at").
		Joins("join shop_window_categories on shop_window.category_id = shop_window_categories.id").
		Joins("join user on shop_window.user_id = user.id").
		Where("shop_window.category_id = ?", categoryId).
		Where("shop_window.name like ?", "%"+name+"%").
		Scopes(Paginate(page, pageSize)).
		Find(&shopWindowsList).Error
	return
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

// 商品关联橱窗添加
func (gsw *GoodsShopWindow) CreateGoodShopWindow(db *gorm.DB) error {
	return db.Create(gsw).Error
}

// 添加橱窗分类
func (gsw *GoodsShopWindow) UpdateShopWindowsType(db *gorm.DB, shopWindowId, typeId int) error {
	return db.Model(&ShopWindow{}).Where("id=?", shopWindowId).Update("category_id", typeId).Error
}

// 审核橱窗商品的状态
func (gsw *GoodsShopWindow) UpdateGoodsShopWindowStatus(db *gorm.DB, shopWindowId, typeId, checkStatus int) error {
	return db.Model(&GoodsShopWindow{}).Where("shop_window_id = ? and goods_id = ?", shopWindowId, typeId).Update("check_status", checkStatus).Error
}

func (g *Goods) CreateGoods(db *gorm.DB) error {
	return db.Create(g).Error
}
